// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.yuanlone.muhabet;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.Toast;

import com.netease.nimlib.sdk.NotificationFoldStyle;
import com.netease.nimlib.sdk.SDKOptions;
import com.netease.nimlib.sdk.ServerAddresses;
import com.netease.nimlib.sdk.StatusBarNotificationConfig;
import com.netease.nimlib.sdk.StatusBarNotificationFilter;
import com.netease.nimlib.sdk.mixpush.MixPushConfig;
import com.yuanlone.muhabet.main.MainActivity;
import com.yuanlone.muhabet.main.mine.setting.ServerConfigUtils;
import com.yuanlone.muhabet.push.PushUserInfoProvider;
import com.yuanlone.muhabet.utils.Constant;
import com.yuanlone.muhabet.utils.DataUtils;
import com.netease.yunxin.kit.alog.ALog;
import com.netease.yunxin.kit.common.utils.ScreenUtils;

import org.json.JSONObject;

/**
 * Nim SDK config info
 */
public class NimSDKOptionConfig {

    private static final String TAG = "NimSDKOptionConfig";
    // 通知音频
    public static final String NOTIFY_SOUND_KEY =
            "android.resource://com.yuanlone.muhabet/raw/msg";
    public static final int LED_ON_MS = 1000;
    public static final int LED_OFF_MS = 1500;

    public static boolean privateConfigServer = false;

    // 初始化SDK配置，参数还以可以参考官网 IM SDK接口文档中有详细说明每个字段含义
    static SDKOptions getSDKOptions(Context context) {

        SDKOptions options = new SDKOptions();
        privateConfigServer = DataUtils.getServerPrivateConfigSwitch(context);
        if (privateConfigServer) {
            String serverConfig = DataUtils.getServerConfig(context);
            ALog.d(TAG, "getSDKOptions", "ServerConfig:" + serverConfig);
            try {
                JSONObject configJson = new JSONObject(serverConfig);
                options.appKey = configJson.getString("appkey");
                ALog.d(TAG, "getSDKOptions", "ServerConfig appkey:" + options.appKey);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (TextUtils.isEmpty(options.appKey)) {
            options.appKey = DataUtils.readAppKey(context);
        }
        initStatusBarNotificationConfig(options);
        options.preloadAttach = true;
        options.thumbnailSize = (int) (222.0 / 375.0 * ScreenUtils.getDisplayWidth());
        options.userInfoProvider = new PushUserInfoProvider(context);
        options.sessionReadAck = true;
        options.animatedImageThumbnailEnabled = true;
        options.asyncInitSDK = true;
        options.reducedIM = false;
        options.checkManifestConfig = false;
        options.enableTeamMsgAck = true;
        options.enableFcs = false;
        options.shouldConsiderRevokedMessageUnreadCount = true;
        // 会话置顶是否漫游
        options.notifyStickTopSession = true;
        options.mixPushConfig = buildMixPushConfig();
        options.serverConfig = configServer(context);
        // 打开消息撤回未读数-1的开关
        options.shouldConsiderRevokedMessageUnreadCount = true;
        return options;
    }

    // 配置海外节点，用户可以参考配置，来进行海外节点配置或者私有化配置
    public static ServerAddresses configServer(Context context) {

        if (DataUtils.getServerPrivateConfigSwitch(context)) {
            String serverConfig = DataUtils.getServerConfig(context);
            ALog.d(TAG, "ServerConfig", "ServerConfig:" + serverConfig);
            return ServerConfigUtils.parseAddresses(serverConfig);
        }

        if (DataUtils.getServerConfigType(context) == Constant.OVERSEA_CONFIG) {
            ServerAddresses serverAddresses = new ServerAddresses();
            serverAddresses.lbs = "https://lbs.netease.im/lbs/conf.jsp";
            serverAddresses.nosUploadLbs = "http://wannos.127.net/lbs";
            serverAddresses.nosUploadDefaultLink = "https://nosup-hz1.127.net";
            serverAddresses.nosDownloadUrlFormat = "{bucket}-nosdn.netease.im/{object}";
            serverAddresses.nosUpload = "nosup-hz1.127.net";
            serverAddresses.nosSupportHttps = true;
            ALog.d("ServerAddresses", "ServerConfig:use Singapore node");
            return serverAddresses;
        }
        return null;
    }

    public static void initStatusBarNotificationConfig(SDKOptions options) {
        // load notification
        StatusBarNotificationConfig config = loadStatusBarNotificationConfig();
        // load 用户的 StatusBarNotificationConfig 设置项
        // SDK statusBarNotificationConfig 生效
        config.notificationFilter =
                imMessage ->
                        IMApplication.getForegroundActCount() > 0
                                ? StatusBarNotificationFilter.FilterPolicy.DENY
                                : StatusBarNotificationFilter.FilterPolicy.DEFAULT;
        options.statusBarNotificationConfig = config;
    }

    // config StatusBarNotificationConfig
    public static StatusBarNotificationConfig loadStatusBarNotificationConfig() {
        StatusBarNotificationConfig config = new StatusBarNotificationConfig();
        config.notificationEntrance = MainActivity.class;
        config.notificationSmallIconId = R.mipmap.ic_logo;
        config.notificationColor = Color.parseColor("#3a9efb");
        config.notificationSound = NOTIFY_SOUND_KEY;
        config.notificationFoldStyle = NotificationFoldStyle.ALL;
        config.downTimeEnableNotification = true;
        config.ledARGB = Color.GREEN;
        config.ledOnMs = LED_ON_MS;
        config.ledOffMs = LED_OFF_MS;
        config.showBadge = true;
        return config;
    }

    // 推送配置，包括小米，华为，魅族，FCM，VIVO，OPPO，参考官网文档说明
    private static MixPushConfig buildMixPushConfig() {

        MixPushConfig config = new MixPushConfig();
        // xiaomi
        config.xmAppId = "2882303761520351409";
        config.xmAppKey = "5442035145409";
        config.xmCertificateName = "2882303761520351409";

        // huawei
//    config.hwAppId = "";
//    config.hwCertificateName = "";

        // meizu
//    config.mzAppId = "";
//    config.mzAppKey = "";
//    config.mzCertificateName = "";

        // fcm
        //        config.fcmCertificateName = "";

        // vivo
//    config.vivoCertificateName = "";

        // oppo
//    config.oppoAppId = "";
//    config.oppoAppKey = "";
//    config.oppoAppSercet = "";
//    config.oppoCertificateName = "";
        return config;
    }
}
